
package upload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.e885.util.web.UniqueFileRenamePolicy;
import com.oreilly.servlet.MultipartRequest;
import common.CommonHttpServlet;

/**
 * url:http://127.0.0.1:8080/JSP/upload/renameUpload.jsp<br>
 * 將上傳的檔案名更改為 32 位元唯一的檔案名，這可以解決中文檔案上傳時，檔案名會產生亂碼的解決方式
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */
public class RenameUploadServlet extends CommonHttpServlet
{
	private static final long serialVersionUID = 7858678334419363039L;

	private String SAVE_DIRECTORY = "upload";

	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		String realPath = getServletContext().getRealPath( SAVE_DIRECTORY );

		File file = new File( realPath );

		if( !file.exists() )
			file.mkdir();

		MultipartRequest mr = new MultipartRequest( request, realPath, 5 * 1024 * 1024, new UniqueFileRenamePolicy() );

		String fileName = mr.getFilesystemName( "uploadFile" );

		PrintWriter writer = response.getWriter();
		writer.write( "upload success, file name:" + fileName );
	}
}
